package com.csp.anotation.mng;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 缓存方法到注解的映射
 *
 * @author:chengsp
 * @createTime:2019/8/22 13:55
 */
public class AnnotationCache {
    private static Map<Method, MethodMapping> mappingMap = new ConcurrentHashMap<>();


    public static MethodMapping getMethodMapping(Method method) {
        MethodMapping methodMapping = mappingMap.get(method);
        if (methodMapping == null) {
            methodMapping = new MethodMapping();
            mappingMap.put(method, methodMapping);
        }
        return methodMapping;
    }

    public static void putMethodAnnotation(Method method, Annotation annotation) {
        getMethodMapping(method).setAnnotation(annotation);
    }

    public static Annotation getAnnotation(Method method) {
        return getMethodMapping(method).getAnnotation();
    }

    public static void putMethodReturnType(Method method, ReturnType type) {
        getMethodMapping(method).setReturnType(type);
    }

    public static ReturnType getReturnType(Method method) {
        return getMethodMapping(method).getReturnType();
    }

    public static void putTarget(Method method, Object o) {
        getMethodMapping(method).setTarget(o);
    }

    public static Object getTarget(Method method) {
        return getMethodMapping(method).getTarget();
    }

    public static void putOriginalSql(Method method, String originalSql) {
        getMethodMapping(method).setOriginalSql(originalSql);
    }

    public static String getOriginalSql(Method method) {
        return getMethodMapping(method).getOriginalSql();
    }

    public static void putPreparedSql(Method method, String preparedSql) {
        getMethodMapping(method).setPreparedSql(preparedSql);
    }

    public static String getPreparedSql(Method method) {
        return getMethodMapping(method).getPreparedSql();
    }

    public static void putReturnGenericType(Method method, Class type) {
        getMethodMapping(method).setReturnGenericType(type);
    }

    public static Class getReturnGenericType(Method method) {
        return getMethodMapping(method).getReturnGenericType();
    }

    public static void putParamList(Method method, List<String> paramList) {
        getMethodMapping(method).setParamList(paramList);
    }

    public static List<String> getParamList(Method method) {
        return getMethodMapping(method).getParamList();
    }
}
